{include file='public/head'}

<body>
	<style type="text/css" media="screen">
		header {
			color: black;
		}
	</style>
	<div class="x-body">
		<form id="form" class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label">分类名称</label>
				<div class="layui-input-block">
					<input type="text" id="name" name="name" lay-verify="required|username" autocomplete="off" placeholder="请输入活动分类名称" class="layui-input">
				</div>
			</div>
			<!--<div class="layui-form-item">
				<label class="layui-form-label">权重</label>
				<div class="layui-input-block">
					<input type="number" id="rank" name="rank" lay-verify="required|username" autocomplete="off" placeholder="排序序号，大的在前" class="layui-input">
				</div>
			</div>-->
			<!--<div class="layui-form-item">
				<label class="layui-form-label">状&nbsp;&nbsp;&nbsp;态</label>
				<div class="layui-input-block">
					<input type="radio" name="status" value="1" title="正常" checked>
					<input type="radio" name="status" value="0" title="禁用">
				</div>
			</div>-->
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-filter="add" lay-submit="">添加</button>
				</div>
			</div>

		</form>
	</div>
</body>
{include file='public/foot'}
<script type="text/javascript">
	layui.use(['form', 'layer', 'laydate', 'upload'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer,
			laydate = layui.laydate,
			upload = layui.upload;;

		//自定义验证规则
		form.verify({
			username: function(value) {
//				if(value.length < 5) {
//					return '登陆账号至少得5个字符';
//				}
			},
			pass: [/(.+){5,16}$/, '密码必须5到16位'],
			repass: function(value) {
				if($('#password').val() != value) {
					return '两次密码不一致';
				}
			}
		});
		//普通图片上传
		var uploadInst1 = upload.render({
			elem: '#demo1'
			, url: '/admin/common/upFile'
			, before: function (obj) {
				//预读本地文件示例，不支持ie8
				obj.preview(function (index, file, result) {
					$('#demo1').attr('src', result); //图片链接（base64）
				});
			}
			, done: function (res) {
				//如果上传失败
				if (res.code == 0) {
					return layer.msg('上传失败');
				}
				//上传成功
				$('#test1').val(res.data);
			}
			, error: function () {
				//演示失败状态，并实现重传
				var demoText = $('#demoText1');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function () {
					uploadInst1.upload();
				});
			}
		});
		//开始时间
		laydate.render({
			elem: '#starttime',
			type: 'datetime'
		});
		//结束时间
		laydate.render({
			elem: '#endtime',
			type: 'datetime'
		});
		//监听提交
		form.on('submit(add)', function(data) {
			console.log(data);
			var form = new FormData();
			var t = $('#form').serializeArray();
			console.log(t)
			$.each(t, function() {
				form.append(this.name, this.value);
			});
			$.ajax({
				url: "{:url('admin/Cate/addCate')}",
				type: "post",
				data: form,
				dataType: "json",
				processData: false,
				contentType: false,
				success: function(data) {
					if(data.code == 1) {
						layer.msg(data.msg, {
							icon: 1,
							time: 2000
						});
						setTimeout(function() {
							var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
							parent.layer.close(index); //执行关闭
							parent.location.href = window.parent.location; //返回父级页面
						}, 2000)
					} else {
						layer.msg(data.msg, {
							icon: 2,
							time: 2000
						});
					}
				}
			});
			return false;
		});

	});
</script>
<style type="text/css">
	.layui-upload-img {
    width: 92px;
    height: 92px;
    margin: 0 10px 10px 0;
}
</style>